<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //session for the php for the the start.

class Home extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('user');
		$this->load->model('card');
	}
	
	function index(){
		if($this->session->userdata('logged_in')){
			$session_data = $this->session->userdata('logged_in');
			$this->process_online($session_data['user_id']);
			$data['username'] = $session_data['username'];
			//$data['shuffled_cards'] = $this->card->shuffled_cards();// view all the suffled cards

			$data['content'] = 'home_view';
		    $this->load->view('include/template', $data);
		}
		else{
			$data['content'] = 'login_view';
			$this->load->view('include/template', $data);
		}
	}

	function logout(){
		$session_data = $this->session->userdata('logged_in');
		$this->process_offline($session_data['user_id']);
		$this->session->unset_userdata('logged_in');
		session_destroy();
		redirect('home', 'refresh');
	}

	function guest_game(){
		// if($this->session->userdata('logged_in')){
			$session_data = "Guest";
			$data['username'] = $session_data;
			//$data['shuffled_cards'] = $this->card->shuffled_cards();// view all the cards

			$data['content'] = 'guest_gameplay';
		    $this->load->view('include/template', $data);
		// }
	}
	
	function registration(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', 'User Name', 'trim|required|min_length[3]|xss_clean');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
		
		if($this->form_validation->run() == FALSE){
			$data['content'] = 'register_form';
		    $this->load->view('include/template', $data);
		}
		else{
			$this->user->add_user();
			$data['content'] = 'login_view';
		    $this->load->view('include/template', $data);
		}
	}
	
	function process_online($id){//write the user_id into a filename for indication of online status
		if($id){
			$session_data = $this->session->userdata('logged_in');
			write_file($this->config->item('dir').$id.'.stat', $session_data['username']);
		}  
    }

    function process_offline($id){//delete the user_id into a directory for indication of ofline status
    	if($id){
			unlink($this->config->item('dir').$id.'.stat');
		}
	}

	function list_online(){
		if($this->session->userdata('logged_in')){
			$key = array();
			$data = array();
			$str = "";

			$data = get_filenames($this->config->item('dir'));

			foreach ($data as $filename) {
				$str = substr($filename, 0, strpos($filename, ".stat"));
				$details = $this->user->get_users($str);

				$session_data = $this->session->userdata('logged_in');
				
				if($this->session->userdata('logged_in') != $str AND $str != $session_data['user_id']){
					array_push($key, array(
						'username' => $details[0]['username']
						));
				}

			}
				echo json_encode($key);
		}
	}
	
}
?>